package com.GlobalPaint.app.ui.Mine.setting;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.GlobalPaint.app.R;
import com.GlobalPaint.app.bean.ReturnStatusResultEntity;
import com.GlobalPaint.app.bean.codeBean;
import com.GlobalPaint.app.utils.CommUtility;
import com.GlobalPaint.app.utils.Constants;
import com.GlobalPaint.app.utils.DataManager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PasswordActivity extends AppCompatActivity {

    @BindView(R.id.iv_goback)
    ImageView ivGoback;
    @BindView(R.id.tv_common_center)
    TextView tvCommonCenter;
    @BindView(R.id.tv_fresh)
    TextView tvFresh;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.et_new_password)
    EditText etNewPassword;
    @BindView(R.id.bt_submit)
    Button btSubmit;
    @BindView(R.id.et_oldpassword)
    EditText etOldpassword;
    @BindView(R.id.rl_old)
    RelativeLayout rlOld;
    @BindView(R.id.ve_oid)
    View veOid;
    private String newpasswrod;
    private String oldpasswrod;
    private codeBean bean;
    public static final int GET_PASSWROD = 0x0003;
    private Handler mhandler = new Handler() {
        public void handleMessage(Message message) {
            switch (message.what) {
                case GET_PASSWROD:
                    String url = Constants.JASON_SERVICE_URL + "user/changePassword?userName="
                            + DataManager.userEntity.getUserName() + "&newPwd=" + newpasswrod + "&oldPwd=" + oldpasswrod
                            + "&userId=" + DataManager.userEntity.getUserId();
                    DataManager.getInst().getHttpRequestJsonClass(url, DataManager.userEntity.getCookie(), codeBean.class, new DataManager.Callback() {
                        @Override
                        public void onSuccess(Object o) {
                            bean = (codeBean) o;
                            if (bean.getStatus() == 1) {
                                finish();
                                Toast.makeText(PasswordActivity.this, "修改完成", Toast.LENGTH_LONG).show();
                            } else {
                                Toast.makeText(PasswordActivity.this, bean.getMsg(), Toast.LENGTH_LONG).show();
                            }

                        }

                        @Override
                        public void onFailure(ReturnStatusResultEntity error) {
                            Toast.makeText(PasswordActivity.this, "获取失败", Toast.LENGTH_LONG).show();
                        }
                    });
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        tvCommonCenter.setText("设置密码");
        if (DataManager.userEntity.getIsHavePwd() == 0) {  //代表有密码
            rlOld.setVisibility(View.VISIBLE);
            veOid.setVisibility(View.VISIBLE);
        } else if (DataManager.userEntity.getIsHavePwd() == 1) {
            rlOld.setVisibility(View.GONE);
            veOid.setVisibility(View.GONE);
        }
    }

    private void newPassword() {
        String password = String.valueOf(etPassword.getText());
        if (TextUtils.isEmpty(password)) {
            CommUtility.ShowMsgShort(this, "请输入新密码");
            return;
        } else {
            if (password.length() < 6 || password.length() > 20) {
                CommUtility.ShowMsgShort(this, "请输入6-20位密码");
                return;
            }
        }
        newpasswrod = String.valueOf(etNewPassword.getText());
        if (TextUtils.isEmpty(newpasswrod)) {
            CommUtility.ShowMsgShort(this, "请确认新密码");
            return;
        } else {
            if (!newpasswrod.equals(password)) {
                CommUtility.ShowMsgShort(this, "两次输入密码不相同，请重新输入");
                return;
            }
        }
        oldpasswrod = String.valueOf(etOldpassword.getText());
        mhandler.sendEmptyMessage(GET_PASSWROD);
    }

    private void oldPassword() {
        oldpasswrod = String.valueOf(etOldpassword.getText());
        if (TextUtils.isEmpty(oldpasswrod)) {
            CommUtility.ShowMsgShort(this, "请输入原始密码");
            return;
        } else {
            if (oldpasswrod.length() < 6 || oldpasswrod.length() > 20) {
                CommUtility.ShowMsgShort(this, "请输入6-20位密码");
                return;
            }
        }
        String password = String.valueOf(etPassword.getText());
        if (TextUtils.isEmpty(password)) {
            CommUtility.ShowMsgShort(this, "请输入新密码");
            return;
        } else {
            if (password.equals(oldpasswrod)) {
                CommUtility.ShowMsgShort(this, "新密码不能和原始密码相同");
                return;
            }
        }
        newpasswrod = String.valueOf(etNewPassword.getText());
        if (TextUtils.isEmpty(newpasswrod)) {
            CommUtility.ShowMsgShort(this, "请确认新密码");
            return;
        } else {
            if (!newpasswrod.equals(password)) {
                CommUtility.ShowMsgShort(this, "两次输入密码不相同，请重新输入");
                return;
            }
        }
        mhandler.sendEmptyMessage(GET_PASSWROD);
    }

    @OnClick({R.id.iv_goback, R.id.bt_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_goback:
                finish();
                break;
            case R.id.bt_submit:
                if (DataManager.userEntity.getIsHavePwd() == 0) {  //代表有密码
                    oldPassword();
                } else if (DataManager.userEntity.getIsHavePwd() == 1) {
                    newPassword();
                }
                break;
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
